package com.dinghui.ebookbag.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Jmh
 * on 2021-01-13
 * 日期格式工具类
 */
public class DateUtils {

    //获取当前时间的 "yyyy-MM-dd"格式字符串
    public static String getNowDateString() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(new Date());
    }

    //获取当前时间的 "yyyy-MM-dd HH:mm:ss"格式字符串
    public static String getNowDateTimeString() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }

    //获取当前时间到凌晨0点的毫秒数
    public static Long getNowIntervalZeroHourMillis() {
        long nowmilli = Calendar.getInstance().getTimeInMillis();

        String nextDay = transformMillisecondToDate(nowmilli + 24 * 60 * 60 * 1000);

        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(nextDay + " 00:00:01"));
            long zeroMilli = calendar.getTimeInMillis();
            return zeroMilli - nowmilli;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0L;
    }

    //将毫秒数转成日期字符串格式
    public static String transformMillisecondToDate(long millisecond) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        date.setTime(millisecond);
        String formatStr = f.format(date);
        return formatStr;
    }

    //获取当前时间毫秒数
    public static long getNowMillisecond() {
        long nowmilli = Calendar.getInstance().getTimeInMillis();
        return nowmilli;
    }

    //字符串格式时间转成毫秒  dateTime:yyyy-MM-dd HH:mm:ss
    public static long stringToMillisecond(String dateTime) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return calendar.getTimeInMillis();
    }

    //获取日期的周次  dateTime:yyyy-MM-dd
    public static int getDateWeek(String dateStr) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr + " 00:00:01"));
            int w = calendar.get(calendar.DAY_OF_WEEK);
            --w;
            w = w == 0 ? 7 : w;
            return w;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //比较开始时间与结束时间的大小 格式为:yyyy-MM-dd HH:mm:ss
    public static Boolean compareTowTimes(String startTime, String endTime) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime));
            long stMilli = calendar.getTimeInMillis();
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime));
            long edMilli = calendar.getTimeInMillis();
            return stMilli > edMilli ? true : false;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    //获取两个日期之间的间隔天数
    public static Integer getIntervalOfTowDate(String startDate, String endDate) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startDate + " 00:00:00"));
            long stMilli = calendar.getTimeInMillis();
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDate + " 00:00:00"));
            long edMilli = calendar.getTimeInMillis();
            long oneDay = 24 * 60 * 60 * 1000;
            edMilli += oneDay;
            long intervalMilli = edMilli - stMilli;

            return (int) (intervalMilli / oneDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //获取两个日期之间的间隔日期
    public static List<String> getIntervalDateListsOfTowDate(String startDate, String endDate) {
        List<String> result = new ArrayList<>();
        Integer intervalNum = getIntervalOfTowDate(startDate, endDate);
        //开始日期的毫秒数
        long sm = stringToMillisecond(startDate + " 00:00:00");
        long oneDay = 24 * 60 * 60 * 1000;
        result.add(startDate);
        for (Integer i = 1; i < intervalNum; i++) {
            String day = transformMillisecondToDate(sm + i * oneDay);
            result.add(day);
        }

        return result;
    }

    //获取毫秒数转成 时 分
    public static String milliToHourMinute(Long milli) {
        long oneHourMilli = 60 * 60 * 1000;
        if (milli < 0) {
            return "";
        }
        DecimalFormat df = new DecimalFormat("0.00");//设置保留位数

        String hf = df.format((float) milli / oneHourMilli);
        Double dh = Double.valueOf(hf);
        int hour = (int) Math.floor(dh);
        int minute = (int) ((dh - hour) * 60);
        return hour + "小时" + minute + "分钟";
    }
}
